package me.ymfs.springboot.guice.vo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Objects;

public class RespVO extends JSONObject {

    public static final String CODE = "code";
    public static final String MSG = "msg";
    public static final String DATA = "data";
    public static final String SUCCESS = "1";
    public static final String SUCCESS_MSG = "操作成功";
    public static final String FAILURE = "0";
    public static final String FAILURE_MSG = "操作失败";

    public static RespVO success() {
        return builder().code(SUCCESS).msg(SUCCESS_MSG).build();
    }

    public static RespVO success(String msg) {
        return builder().code(SUCCESS).msg(msg).build();
    }

    public static RespVO success(Object data) {
        return builder().code(SUCCESS).msg(SUCCESS_MSG).data(data).build();
    }

    public static RespVO success(String msg, Object data) {
        return builder().code(SUCCESS).msg(msg).data(data).build();
    }

    public static RespVO failure() {
        return builder().code(FAILURE).msg(FAILURE_MSG).build();
    }

    public static RespVO failure(String msg) {
        return builder().code(FAILURE).msg(msg).build();
    }

    public static RespVO failure(Object data) {
        return builder().code(FAILURE).msg(FAILURE_MSG).data(data).build();
    }

    public static RespVO failure(String msg, Object data) {
        return builder().code(FAILURE).msg(msg).data(data).build();
    }

    public static RespVO respDTO() {
        return builder().build();
    }

    public static RespVO respDTO(String code, String msg) {
        return builder().code(code).msg(msg).build();
    }

    public static RespVO respDTO(String code, String msg, Object data) {
        return builder().code(code).msg(msg).data(data).build();
    }

    public RespVO code(String code) {
        if (Objects.nonNull(code)) this.put(CODE, code);
        return this;
    }

    public RespVO msg(String msg) {
        if (Objects.nonNull(msg)) this.put(MSG, msg);
        return this;
    }

    public RespVO data(Object data) {
        if (Objects.nonNull(data)) this.put(DATA, data);
        return this;
    }

    public String getCode() {
        return this.getString(CODE);
    }

    public String getMsg() {
        return this.getString(MSG);
    }

    public Object getData() {
        return this.get(DATA);
    }

    public <T> T getData(Class<T> clazz) {
        return this.getObject(DATA, clazz);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public static RespDTOBuilder builder() {
        return new RespDTOBuilder();
    }

    public static class RespDTOBuilder {

        private String code;
        private String msg;
        private Object data;

        RespDTOBuilder() {}

        public RespDTOBuilder code(String code) {
            this.code = code;
            return this;
        }

        public RespDTOBuilder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public RespDTOBuilder data(Object data) {
            this.data = data;
            return this;
        }

        public RespVO build() {
            return new RespVO().code(this.code).msg(this.msg).data(this.data);
        }

        @Override
        public String toString() {
            return JSON.toJSONString(this);
        }
    }
}
